/*
 * eventHandler.c
 *
 *  Created on: 2010-3-20
 *      Author: abc
 */
#include <gtk/gtk.h>

#include "chatRoom.h"
extern gchar *titles[6] ;
extern int hostID;
extern GtkTextBuffer *bufferup,*bufferdown;
extern pclientinfoFS clientlinkFS;
extern pclientinfoFC clientlinkFC;
extern int sockfd;
extern pchatRoom pcrMain;
extern GtkWidget* textup;

enum {
	ROOMNAME,
    MEMNUM,
    HOSTNAME,
    DISCRIPTION,
    ISNEEDPASSWD,
    ID,
    N_COLUMNS
};
void list_selection_changed_cb (GtkTreeSelection *selection, gpointer data)
{
        GtkTreeIter iter;
        GtkTreeModel *model;
        int id;

        if (gtk_tree_selection_get_selected (selection, &model, &iter))
        {
                gtk_tree_model_get (model, &iter, ID, &id, -1);

                g_print ("You selected ID: %d\n", id);
                hostID = id;
        }
}


void
add_columns (GtkTreeView *treeview)
{
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  //GtkTreeModel *model = gtk_tree_view_get_model (treeview);

 //
  /* column for roomname */
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes (titles[0],
						     renderer,
						     "text",
						     ROOMNAME,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, ROOMNAME);
  gtk_tree_view_append_column (treeview, column);

  /* column for memnum */
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes (titles[1],
						     renderer,
						     "text",
						     MEMNUM,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, MEMNUM);
  gtk_tree_view_append_column (treeview, column);

  /* column for hostname */
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes (titles[2],
						     renderer,
						     "text",
						     HOSTNAME,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, HOSTNAME);
  gtk_tree_view_append_column (treeview, column);


  /* column for description */
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes (titles[3],
						     renderer,
						     "text",
						     DISCRIPTION,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, DISCRIPTION);
  gtk_tree_view_append_column (treeview, column);


  /* column for passwd */
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes (titles[4],
						     renderer,
						     "text",
						     ISNEEDPASSWD,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, ISNEEDPASSWD);
  gtk_tree_view_append_column (treeview, column);


  /* column for id */
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes (titles[5],
						     renderer,
						     "text",
						     ID,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, ID);
  gtk_tree_view_append_column (treeview, column);

}

void send_messageFS(GtkButton *button, gpointer data){
	struct message a;
	time_t timep;
	GtkTextIter iter1,iter2;
	gtk_text_buffer_get_end_iter(bufferdown,&iter1);
	gtk_text_buffer_get_start_iter(bufferdown,&iter2);
	char* buffer = gtk_text_buffer_get_text(bufferdown,&iter1,&iter2,FALSE);
	char str[MAXLEN];
	time (&timep);
	sprintf(str,"%s %s发给所有人:%s\n\n",ctime(&timep),pcrMain->hostName,buffer);
	gdk_threads_enter();
	gtk_text_buffer_get_end_iter(bufferup,&iter1);
	gtk_text_buffer_insert(bufferup,&iter1,str,-1);
	gtk_text_buffer_set_text(bufferdown,"",0);
	gtk_text_buffer_get_end_iter(bufferup,&iter1);
	GtkTextMark * tmp = gtk_text_buffer_create_mark(bufferup,NULL,&iter1,FALSE);
	gtk_text_view_scroll_to_mark((GtkTextView*)textup,tmp,0.3,TRUE,0.2,0.1);
	gdk_threads_leave();
	pclientinfoFS L;
	L = clientlinkFS;
	L=L->next;
	strcpy(a.flag,"all");
	strcpy(a.name,pcrMain->hostName);
	strcpy(a.msg,buffer);
	printf("%s %s\n",a.name,a.msg);
    while(L != NULL)
	{
		send(L->sockfd,&a,sizeof(struct message),0);
		L = L->next;
	}
	printf("%s says %s\n",a.name,a.msg);
	free(buffer);
}

void send_messageFC(GtkButton *button, gpointer data){
	struct message a;
	time_t timep;
	//printf("0\n");
	GtkTextIter iter1,iter2;
	gtk_text_buffer_get_end_iter(bufferdown,&iter1);
	gtk_text_buffer_get_start_iter(bufferdown,&iter2);
	char* buffer = gtk_text_buffer_get_text(bufferdown,&iter1,&iter2,FALSE);
	char str[MAXLEN];
	struct hostent* h = gethostent();
	time (&timep);
	sprintf(str,"%s %s发给所有人:%s\n\n",ctime(&timep),(char*)h->h_name,buffer);
	//printf("1\n");
	gdk_threads_enter();
	gtk_text_buffer_get_end_iter(bufferup,&iter1);
	gtk_text_buffer_insert(bufferup,&iter1,str,-1);
	gtk_text_buffer_set_text(bufferdown,"",0);
	gtk_text_buffer_get_end_iter(bufferup,&iter1);
	GtkTextMark * tmp = gtk_text_buffer_create_mark(bufferup,NULL,&iter1,FALSE);
								gtk_text_view_scroll_to_mark((GtkTextView*)textup,tmp,0.3,TRUE,0.2,0.1);
				gdk_threads_leave();
	//printf("2\n");
		            strcpy(a.flag,"all");
					strcpy(a.name,(char*)h->h_name);
					strcpy(a.msg,buffer);
					printf("%s says %s\n",a.name,a.msg);

					if(send(sockfd,&a,sizeof(struct message),0)>0){
					    	  printf("send successfully!!!\n");
					    	}


					printf("%s says %s\n",a.name,a.msg);
					free(buffer);
					endhostent();
}
